/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package App1.bookShop.controllers;

import APP1.bookShop.people.Author;
import APP1.bookShop.book.Book;
import APP1.bookShop.main.BookShop;
import javax.swing.DefaultListModel;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Asus
 */
public class AuthorController {

    private BookShop bookShop;

    public AuthorController(BookShop bookShop) {
        this.bookShop = bookShop;
    }

    public String addNewAuthor(String name, String surname) {

        //check input
        if (name == null || surname == null) {
            return "Malformed input";
        }
        name.trim();
        surname.trim();
        if (name.length() <= 0 || surname.length() <= 0) {
            return "Malformed input";
        }

        Author add = new Author(name, surname);


        bookShop.getAuthors().add(add);
        return "Author Added";
    }

    public DefaultTableModel getTableMOdelAuthorList() {
        DefaultTableModel dtm = new DefaultTableModel(new Object[]{"Surname", "Name"}, 0) {

            @Override
            public boolean isCellEditable(int row, int column) {
                return false; //To change body of generated methods, choose Tools | Templates.
            }

        };
        Object[] row;
        for (Author auth : bookShop.getAuthors()) {
            row = new Object[]{auth.getLastName(), auth.getFirstName()};
            dtm.addRow(row);
        }
        return dtm;
    }

    public DefaultListModel getAuthorBookTitleListListModel(Author a) {
        DefaultListModel lm = new DefaultListModel();

        for (Book book : a.getBookList()) {
            lm.addElement(book.getTitle());

        }
        if (lm.size() <= 0) {

            lm.addElement("No Books");
        }
        return lm;
    }

    public DefaultListModel getNONAuthorsOfaBook(Book b) {
        DefaultListModel lm;
        if (b != null) {
             lm = new DefaultListModel();

            for (Author a : bookShop.getAuthors()) {
                if (!b.getAuthors().contains(a)) {
                    lm.addElement(a);
                }

            }
        } else {
            lm = new DefaultListModel();

            for (Author a : bookShop.getAuthors()) {

                lm.addElement(a);

            }
        }

        return lm;
    }

}
